package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.User;
import com.sdry.service.llm.DepartmentService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 部门管理Controller
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:49:32
*/

@Controller
@RequestMapping("/depMana")
public class DepartmentController {
	
	@Resource
	private DepartmentService depService;  //注入部门service
	
	/**
	 * 分页查询部门信息
	 * @param page 分页条件
	 * @param dep 部门信息
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageDep")
	public void listPageDep(Page page, Department dep, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (dep.getName() != null ) {
			map.put("name", dep.getName());
		} else {
			map.put("name", "");
			dep.setName("");
		}
		int count = depService.countDep(dep);
		List<Department> list = depService.listPageDep(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 分页查询部门员工信息
	 * @param page 分页条件
	 * @param dep 部门信息
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageUserByDepartment")
	public void listPageUserByDepartment(Page page, Department dep, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (null != dep.getDid()) {
			map.put("did", dep.getDid());
		} else {
			map.put("did", 0L);
			dep.setDid(0L);
		}
		int count = depService.countUserByDep(dep);
		List<User> list = depService.listPageUserByDepartment(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存部门信息
	 * @param dep 部门信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveDep")
	public void saveDep(Department dep, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (dep.getDid() == null || "".equals(dep.getDid())) {
			isSuccess = depService.insertDep(dep);
		} else {
			isSuccess = depService.updateDep(dep);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除部门
	 * @param str did组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteDep")
	public void deleteDep(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = depService.deleteDep(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除部门
	 * @param str did组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteDepOfVerify")
	public void deleteDepOfVerify(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = depService.deleteDepOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除部门员工
	 * @param emp 
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUserInEmp")
	public void deleteUserInEmp(Employee emp, HttpServletResponse response) throws Exception{
		int isSuccess = depService.deleteUserInEmp(emp);
		ResponseUtil.write(response, isSuccess);
	}
}
